/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.HisDiagnosis;
import com.jims.master.modules.doctor.entity.LabApply;
import com.jims.master.modules.doctor.entity.LabItem;
import com.jims.master.modules.doctor.service.ClinicRecordService;
import com.jims.master.modules.doctor.service.HisDiagnosisService;
import com.jims.master.modules.doctor.service.LabApplyService;
import com.jims.master.modules.doctor.service.LabItemService;
import com.jims.master.modules.oper.entity.LabType;
import com.jims.master.modules.oper.service.LabTypeService;
import com.jims.master.modules.orders.entity.Orders;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 检验申请Controller
 * @author 陈晓阳
 * @version 2017-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/lab/labApply")
public class LabApplyController extends BaseController {

	@Autowired
	private LabApplyService labApplyService;
    @Autowired
    private LabTypeService labTypeService;//检验项目Service
    @Autowired
    private LabItemService labItemService;
    @Autowired
    private OrgDeptService orgDeptService;
    @Autowired
    private HisDiagnosisService hisDiagnosisService;
    @Autowired
    private ClinicRecordService clinicRecordService;

    @ModelAttribute
	public LabApply get(@RequestParam(required=false) String id) {
		LabApply entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = labApplyService.get(id);
		}
		if (entity == null){
			entity = new LabApply();
		}
		return entity;
	}

    /**
     * 检验申请主页面
     * @param request
     * @param response
     * @param model
     * @return
     * @author 陈晓阳
     */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(LabApply labApply,HttpServletRequest request, HttpServletResponse response, Model model) {
        //1 查询检验项目
        LabType labType= new LabType();
        labType.setLevel("1");
        List<LabType> labTypeList=this.labTypeService.findList(labType);
        model.addAttribute("labTypeList", labTypeList);
        String returnPath="";
        if(StringUtils.isNotBlank(labApply.getClinicId())){
            labApply.setInoroutFlag("0");
            List<HisDiagnosis> hisDiagnosisList = hisDiagnosisService.getByClinicId(labApply.getClinicId(),"");

            String count = "";
            String chineseMedicine = "";
            String westernMedicine = "";
            String hisDiagnosisDes1 = "";
            String hisDiagnosisDes2 = "";
            for(int i = 0; i<hisDiagnosisList.size(); i++){
                if("0".equals(hisDiagnosisList.get(i).getType())){
                    String diagType1 = "\r\n中医: \n";
                    hisDiagnosisDes1 +="      "+hisDiagnosisList.get(i).getIcdName()+";\n";
                    chineseMedicine = diagType1 +hisDiagnosisDes1;
                } else if("1".equals(hisDiagnosisList.get(i).getType())){
                    String diagType2 = "西医: \n";
                    hisDiagnosisDes2 +="      "+hisDiagnosisList.get(i).getIcdName()+";\n";
                    westernMedicine = diagType2 +hisDiagnosisDes2;
                }
                count = chineseMedicine +westernMedicine ;
            }
            model.addAttribute("hisDiagnosisDes",count);
            returnPath="modules/doctor/lab/labApplyIndex";
        }else{
            labApply.setInoroutFlag("1");
            returnPath="modules/doctor/lab/in/labApplyIndex";
        }

        LabItem labItem = new LabItem();
        labItem.setLabApply(labApply);
        Page<LabApply> page = this.labApplyService.findPage(new Page<LabApply>(request, response), labApply);
        model.addAttribute("page",page);
        model.addAttribute("labItem",labItem);
        model.addAttribute("labApply",labApply);
        return returnPath;
	}
      /**
     * 检验申请保存
     * @param labItem
     * @param model
     * @return
     * @author 陈晓阳
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(LabItem labItem, Model model) {
	    StringData data=new StringData();
		int i=labApplyService.saveLabApplyData(labItem);
        if(i>0){
            data.setCode("success");
            data.setData("保存检验申请成功");
        }else{
            data.setCode("error");
            data.setData("保存检验申请失败");
        }
        return data;
	}

    /**
     * 删除检验申请
     * @param labApply
     * @return
     * @author 陈晓阳
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(LabApply labApply) {
	    StringData data=new StringData();
		int i=labApplyService.deleteAllData(labApply);
        if(i>0){
            data.setCode("success");
            data.setData("删除检验申请成功");
        }else{
            data.setCode("error");
            data.setData("删除检验申请失败");
        }
		return data;
	}

     /**
     * 条件查询病人检验申请
     * @param model
     * @param request
     * @param response
     * @return
     * @Author 陈晓阳
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"queryByCondition", ""})
    public String queryByCondition(LabApply labApply,Model model,HttpServletRequest request, HttpServletResponse response) {
        //病人Id 和检查项目名称
        Page<LabApply> page = labApplyService.findPage(new Page<LabApply>(request, response), labApply);
        model.addAttribute("page",page);
        if("0".equals(labApply.getInoroutFlag())){
            return "modules/doctor/lab/labApplyRight";
        }else{
            return "modules/doctor/lab/in/labApplyRight";
        }
    }

    /**
     * 根据检验项目id查找其诊疗项目集合
     * @param labType
     * @param request
     * @param response
     * @return
     * @Author 陈晓阳
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"queryBylabTypeId", ""})
    @ResponseBody
    public List<LabType> queryByPId(LabType labType,HttpServletRequest request, HttpServletResponse response) {

        List<LabType> list = this.labTypeService.queryBylabTypeId(labType.getId());
        return list;
    }

    /**
     * 检验确认页面数据
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "labConfirm")
    public String examConfirm(LabApply labApply,Model model,HttpServletRequest request, HttpServletResponse response){
        if(labApply.getConfirmFlag()==null){
            labApply.setConfirmFlag("0");
        }
        if("0".equals(labApply.getInoroutFlag())){
            labApply.setChargeFlag("1");
        }
        Page<LabApply> page = this.labApplyService.findPage(new Page<LabApply>(request, response), labApply);
        model.addAttribute("page",page);
        model.addAttribute("labApply",labApply);
        return "modules/doctor/lab/labConfirm";
    }

    /**
     * 执行确认操作
     * @param labApply
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "labConfirmDo")
    @ResponseBody
    public StringData examConfirmDo(LabApply labApply) {
        StringData data=new StringData();
        int i=labApplyService.labConfirmDo(labApply);
        if(i>0){
            data.setCode("success");
            data.setData("确认检验申请成功");
        }else{
            data.setCode("error");
            data.setData("确认检验申请失败");
        }
        return data;
    }

    /**
     * 取消执行确认
     * @param labApply
     * @return
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "cancelExamConfirmDo")
    @ResponseBody
    public StringData cancelExamConfirmDo(LabApply labApply){
        StringData data=new StringData();
        int i=labApplyService.cancelLabConfirmDo(labApply);
        if(i>0){
            data.setCode("success");
            data.setData("确认检验申请成功");
        }else{
            data.setCode("error");
            data.setData("确认检验申请失败");
        }
        return data;
    }
    /**
     * 检验诊间收费数据
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "waitFeeData")
    public String waitFeeData(LabApply labApply,Model model,HttpServletRequest request, HttpServletResponse response){
         labApply.setChargeFlag("0");
         labApply.setInoroutFlag("0");
         labApply.setImplementDept(UserUtils.getUser().getOrgDept().getId());
         Page<LabApply> page = this.labApplyService.findPage(new Page<LabApply>(request, response), labApply);
         model.addAttribute("page",page);
         model.addAttribute("labApply",labApply);
         return "modules/doctor/lab/waitFeeData";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "printPresc")
    @ResponseBody
    public StringData printPresc(String clinicId,String visitId,String patientId,String labId){
        StringData stringData = new StringData();
        String printPath = labApplyService.printPresc(clinicId,visitId,patientId,labId);
        stringData.setData(printPath);
        stringData.setCode("success");
        return stringData;
    }


    /**
     * 住院申请退费
     * @param model
     * @return
     *@Author zp
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "outpRetCharge")
    @ResponseBody
    public StringData outpRetCharge(LabApply labApply, Model model) {
        StringData data=new StringData();
        int i=labApplyService.inpRetCharge(labApply);
        if(i>0){
            data.setCode("success");
            data.setData("申请成功");
        }else{
            data.setCode("error");
            data.setData("申请失败");
        }
        return data;
    }


    /**
     * 住院确认退费
     * @param Orders
     * @param model
     * @param visitNo
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "retChargeOk")
    @ResponseBody
    public StringData retChargeOk(Orders orders, Model model, String visitNo) {
        StringData stringData = new StringData();
        int num = labApplyService.retChargeOk(orders);
        if(num>0){
            stringData.setCode("success");
            stringData.setData("退费成功");
        }else{
            stringData.setCode("error");
            stringData.setData("退费失败");
        }
        return stringData;


    }

}